// 登录成功
class Index{
    constructor(){
        this.login = document.querySelector(".h_login");
        this.wel = document.querySelector(".wel");
        this.logout = document.getElementById("logout");
        this.uName = document.getElementById("userName");
        this.shopcar = document.querySelector(".shopcar");
        this.init();
        this.addEvent();
    }
    init(){
        var that = this;
        this.m = sessionStorage.getItem("userMsg");
        this.m = this.m ? JSON.parse(this.m) : {};
        if(this.m.user){
            this.login.style.display = "none";
            this.wel.style.display = "none";
            this.logout.style.display = "block";
            this.uName.style.display = "block";
            this.uName.innerHTML = this.m.user;
        }
        this.shopcar.onclick = function () {
            if(that.m.user){
                location.href = "shopcar.html"
            }else{
                location.href = "register.html"
            }
        }

    }
    addEvent(){
        this.logout.onclick = function(){
            sessionStorage.removeItem("userMsg");
            location.reload();
        }
    }
}
new Index;


// 数据的渲染
class List{
    constructor(){
        this.url = "http://localhost:666/static/data/goods.json";
        this.contList = document.querySelector(".contList");

        this.load();
    }
    load(){
        ajax({
            url:this.url
        }).then((res)=>{
            this.res = JSON.parse(res);
            this.display();
        })
    }
    display(){
        // console.log(this.res);
        let str = "";
        for(var i=0;i<this.res.length;i++){
            str += `<li index="${this.res[i].goodsId}">
                        <a href="goodsDetail.html?id=${this.res[i].goodsId}">
                            <div class="box">
                                <p>
                                    <img src="${this.res[i].img.largeImg[0]}" alt="">     
                                </p>
                                <p class="name">
                                    ${this.res[i].name}
                                </p>
                                <p class="price">
                                    ￥${this.res[i].price}
                                </p>
                            </div>
                        </a> 
                    </li>`;
        }
        this.contList.innerHTML = str;
    }
}
new List;


